package luj.game.robot.internal.dynamic.init;

import java.util.List;

import luj.game.robot.api.action.RobotBossCommand;
import luj.game.robot.api.action.RobotCreateListener;
import luj.game.robot.api.action.RobotDataCommand;
import luj.game.robot.api.boot.RobotStartListener;
import luj.game.robot.api.network.http.RobotHttpHandler;
import luj.game.robot.api.network.http.RobotHttpMeta;
import luj.game.robot.api.network.http.RobotHttpModule;
import luj.game.robot.api.plugin.JambotDynamicInit;
import luj.game.robot.api.network.RobotProtoHandler;

public class DynamicInitInvoker {

  public interface Result {

    List<RobotStartListener> startListener();

    List<RobotCreateListener> createListener();

    List<RobotBossCommand<?>> bossCommand();

    List<RobotDataCommand<?>> dataCommand();

    List<RobotProtoHandler<?>> protoHandler();

    List<RobotHttpHandler> httpHandler();

    List<RobotHttpMeta> httpMeta();

    List<RobotHttpModule> httpModule();
  }

  public DynamicInitInvoker(JambotDynamicInit initPlugin, Object startParam) {
    _initPlugin = initPlugin;
    _startParam = startParam;
  }

  public Result invoke() {
    var ctx = new ContextImpl();
    ctx._startParam = _startParam;

    // 调用外部初始化逻辑
    _initPlugin.onInit(ctx);

    return makeResult(ctx);
  }

  private Result makeResult(ContextImpl ctx) {
    var result = new ResultImpl();
    result._start = ctx.findBeans(RobotStartListener.class);
    result._create = ctx.findBeans(RobotCreateListener.class);

    result._bossCommand = ctx.findBeans(RobotBossCommand.class);
    result._dataCommand = ctx.findBeans(RobotDataCommand.class);
    result._protoHandler = ctx.findBeans(RobotProtoHandler.class);

    result._httpHandler = ctx.findBeans(RobotHttpHandler.class);
    result._httpMeta = ctx.findBeans(RobotHttpMeta.class);
    result._httpModule = ctx.findBeans(RobotHttpModule.class);

    return result;
  }

  private final JambotDynamicInit _initPlugin;

  private final Object _startParam;
}
